﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.IO;

namespace NivdalDashboard
{
    class GetNivdalData
    {
        public class ParamValue
        {
            public string Name { get; set; }
            public object Value { get; set; }
            public System.Data.SqlDbType Type { get; set; }
            public int Size { get; set; }
        }

        public static bool execSP(List<ParamValue> ParamsIn, List<ParamValue> Paramsout, string SPName)
        {
            bool success = true;

            using (SqlConnection conn = new SqlConnection(Properties.Resources.NivdalConnectionString))
            {
                SqlCommand cmd = new SqlCommand(SPName, conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                
                foreach (ParamValue p in ParamsIn)
                {
                    cmd.Parameters.AddWithValue(p.Name, p.Value);
                }

                foreach (ParamValue p in Paramsout)
                {
                    cmd.Parameters.Add(p.Name, p.Type, p.Size);
                    cmd.Parameters[p.Name].Direction = System.Data.ParameterDirection.Output;
                }

                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    foreach (ParamValue p in Paramsout)
                    {
                        p.Value = cmd.Parameters[p.Name].Value;
                    }
                }
                catch (Exception ex)
                {
                    LogMessage(ex.Message);
                    success = false;
                }
                finally
                {
                    conn.Close();
                }
            }

            return success;
        }

        public static void RecompileTransferMarketSP()
        {
            using (SqlConnection conn = new SqlConnection(Properties.Resources.NivdalConnectionString))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "EXEC sp_recompile N'HumanResources.uspGetAllEmployees'";
                cmd.CommandType = System.Data.CommandType.Text;
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    LogMessage(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        public static void LogMessage(string message)
        {
            bool debug = true;
            if (debug)
            {
                string FileName = "DashboardLog.txt";
                TextWriter tw = new StreamWriter(FileName, true);
                tw.WriteLine(DateTime.Now.ToString());
                tw.WriteLine(message);
                tw.WriteLine("--------------------------------");
                tw.Close();
            }
        }
    }
}
